package com.smart.thirteen.rowmapper;

import com.smart.thirteen.Topic;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static javafx.scene.input.KeyCode.R;

/**
 * @author junrui.wang
 * @create 2019/10/21
 */
public class RowMapperTest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:com/smart/thirteen/applicationContext-template.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate");

        //处理多条数据集
        String sql2 = "SELECT id, topic_title, user_id FROM t_topic WHERE id BETWEEN ? AND ?";
        List<Topic> topics =jdbcTemplate.query(sql2, new Object[]{1, 3}, new RowMapper<Topic>() {

            @Override
            public Topic mapRow(ResultSet rs, int rowNum) throws SQLException {
                Topic topic = new Topic();
                topic.setId(rs.getInt(1));
                topic.setTopic_title(rs.getString(2));
                topic.setUser_id(rs.getString(3));
                return topic;
            }});

        System.out.println(topics);



    }
}
